package edu.nyp.locationservicesapp.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import edu.nyp.locationservicesapp.R;
import edu.nyp.locationservicesapp.application.LauncherModel;
import edu.nyp.locationservicesapp.constant.IPreferencesIds;
import edu.nyp.locationservicesapp.utils.PermissionUtils;
import edu.nyp.locationservicesapp.utils.ToastUtil;

public class EventLocationActivity extends AppCompatActivity{

    private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_location);
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            // Permission to access the location is missing.
            PermissionUtils.requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE,
                    Manifest.permission.ACCESS_FINE_LOCATION, true);
        }
        Intent intent = getIntent();
        boolean showWelcome = intent.getBooleanExtra("showWelcome",false);
        if(showWelcome){
            LinearLayout llWelcome = (LinearLayout)findViewById(R.id.ll_welcome);
            TextView tvWelcome = (TextView)findViewById(R.id.tv_welcome_user);
            llWelcome.setVisibility(View.VISIBLE);
            String userName = LauncherModel.getInstance().getSPManager().getString(IPreferencesIds.USER_LOGIN_PHONE, "");
            tvWelcome.setText(userName);
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) {
            return;
        }
        if (PermissionUtils.isPermissionGranted(permissions, grantResults, Manifest.permission.ACCESS_FINE_LOCATION)) {
            ToastUtil.showShortToast(this, getString(R.string.location_permission_denied));
        }
    }

    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btnShow:
                Intent intent = new Intent(this, MapsActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.activity_in_from_right, R.anim.activity_out_from_left);
                break;
            case R.id.btnAbout:
                intent = new Intent(this, AboutUsActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.activity_in_from_right, R.anim.activity_out_from_left);
                break;
            case R.id.btnLogout:
                LauncherModel.getInstance().getSPManager().commitBoolean(IPreferencesIds.USER_IS_LOGIN, false);
                intent = new Intent(this, LoginActivity.class);
                startActivity(intent);
                finish();
                overridePendingTransition(R.anim.activity_in_from_right, R.anim.activity_out_from_left);
                break;
        }
    }
}
